package com.miracle9.game.dao;

import com.miracle9.game.entity.FishDesk;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@Repository
public class FishDeskDao extends  SqlOperating<FishDesk, Integer> {

    private ConcurrentHashMap<Integer, FishDesk> concurrentHashMap = new ConcurrentHashMap<>();
    private ConcurrentHashMap<Integer, List<FishDesk>> concurrentListHashMap = new ConcurrentHashMap<>();



    public void reloadDesk() {
        List<FishDesk> liangxiDesk = new ArrayList<>();
        List<FishDesk> jingjiDesk = new ArrayList<>();
        for (FishDesk f : this.concurrentHashMap.values()) {
            if (f.getRoomId() == 1) {
                liangxiDesk.add(f);
            } else if (f.getRoomId() == 2) {
                jingjiDesk.add(f);
            }
        }
        Collections.sort(liangxiDesk, new Comparator<FishDesk>() {
            public int compare(FishDesk o1, FishDesk o2) {
                return o1.getOrderBy() - o2.getOrderBy();
            }
        });
        Collections.sort(jingjiDesk, new Comparator<FishDesk>() {
            public int compare(FishDesk o1, FishDesk o2) {
                return o1.getOrderBy() - o2.getOrderBy();
            }
        });
        this.concurrentListHashMap.put(1, liangxiDesk);
        this.concurrentListHashMap.put(2, jingjiDesk);
    }

    public List<FishDesk> getDeskListByRoomId(int roomId) {
        return this.concurrentListHashMap.get(roomId);
    }

    public FishDesk getFishDesk(int id) {
        return this.concurrentHashMap.get(id);
    }

    public List<FishDesk> getDeskListByHallId(int hallId) {
        List<FishDesk> desks = new ArrayList<>();
        for (FishDesk f : this.concurrentHashMap.values()) {
            if (f.getHallId() == hallId) {
                desks.add(f);
            }
        }
        return desks;
    }

    public List<FishDesk> getDeskList() {
        List<FishDesk> desks = new ArrayList<>();
        for (FishDesk f : this.concurrentHashMap.values()) {
            desks.add(f);
        }
        return desks;
    }




    public void removeDesk(int deskId) {
        FishDesk deleteDesk = this.concurrentHashMap.remove(deskId);
        this.concurrentListHashMap.get(deleteDesk.getRoomId()).remove(deleteDesk);
    }

    public void cleanDesk(int deskId) {
        FishDesk desk = getFishDesk(deskId);
        synchronized (desk) {
            desk.setSumDeFen(0L);
            desk.setSumYaFen(0L);
            desk.setSumSeat1DeFen(0L);
            desk.setSumSeat2DeFen(0L);
            desk.setSumSeat3DeFen(0L);
            desk.setSumSeat4DeFen(0L);
            desk.setSumSeat1YaFen(0L);
            desk.setSumSeat2YaFen(0L);
            desk.setSumSeat3YaFen(0L);
            desk.setSumSeat4YaFen(0L);
        }
    }

}
